import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

// 数据持久化
const localEvent = function(item) {
	this.get = function() {
		let result = uni.getStorageSync(item);
		if(typeof result != 'undefined' && result!=''){			
			return JSON.parse(uni.getStorageSync(item));
		}else{
			return '';
		}
	}
	this.set = function(obj) {
		uni.setStorageSync(item, JSON.stringify(obj));
	}
	this.clear = function() {
		uni.removeStorageSync(item);
	}
}
const local = new localEvent('mmno');

const state = local.get() || {
	testvalue: 'hello',
	cartCounter: 0, //商品总数量 
	allMoney: 0, //总价
	allSelsect: true,
	cart: [
		// {id: 1, image: 'image.jpg',title:'苹果', price: 200, quantity : 1, selected: true}, //示例
	]
}

const store = new Vuex.Store({
	state,
	mutations: {
		changeTest(state, value) {
			state.testvalue = value;
		},

		//添加商品
		ADDGOODS(state, data) {
			let goodsItem = { //传入的商品属性
				productId: data.id,
				title: data.title,
				image: data.image,
				price: data.price,
				quantity: 1,
				selected: true
			}
			let index = -1
			index = state.cart.findIndex(item => { //返回元素的索引位置，如果没有符合条件的元素返回 -1
				return item.productId == data.id
			})
			if (index == -1) { //返回-1说明购物车里没有这个商品，则push进去，如果返回下标，则让此商品的数量加1
				state.cart.push(goodsItem)
			} else {
				state.cart[index].quantity++
			}
			state.cartCounter++;
			local.set(state);
		},

		//购物车页面数量加减
		ADDNUM(state, productId) {
			let index = state.cart.findIndex(item => {
				return item.productId == productId
			})
			state.cartCounter++;
			
			state.cart[index].quantity++
			
			local.set(state);
			
			 
		},
		//购物车页面数量加减
		REDUCENUM(state, productId) {
			let index = state.cart.findIndex(item => {
				return item.productId == productId
			})
			if (state.cart[index].quantity <= 1) {
				state.cart[index].quantity = 1 // 简单的容错
			} else {
				state.cartCounter--;
				state.cart[index].quantity--
			}
			local.set(state);
		},

		// 删除商品
		DELGOODS(state, productId) {

			state.cartCounter--;

			if (state.cartCounter < 0) {
				state.cartCounter = 0
			}

			state.cart.some((val, index, Goods) => {
				if (val.productId === productId) {
					val.quantity--;
					Goods.splice(index, 1);
					return true;
				}
			})
			local.set(state);
		},

		//购物车单选
		SELECT(state, productId) {
			let index = state.cart.findIndex(item => {
				return item.productId == productId
			})
			state.cart[index].selected = !state.cart[index].selected
			let flag = state.cart.some((item) => {
				return item.selected == false
			})
			if (!flag) { //如果全部选中，则全选状态也改变
				state.allSelsect = true
			} else {
				state.allSelsect = false
			}
			local.set(state);
		},

		// 购物车全选
		SELECTALL(state) {
			state.allSelsect = !state.allSelsect
			if (state.allSelsect) {
				state.cart.forEach((item) => {
					item.selected = true
				})
			} else {
				state.cart.forEach((item) => {
					item.selected = false
				})
			}
			local.set(state);
		},

		//删除选中的商品，注意这里不能使用数组删除的方法，索引值会有问题
		CLEAR_SELECTED(state) {

			var list = [];
			state.cart.forEach((item, index) => {
				if (!item.selected) {
					list.push(item)
				} else {
					state.cartCounter = state.cartCounter - item.quantity;
				}
			})			

			state.cart = list
			
			local.set(state);

		},

	},
	getters: {

		//选中商品的金额
		allMoney(state) {
			let all_money = 0
			state.cart.forEach((item) => {
				if (item.selected) {
					all_money += item.quantity * item.price
				}
			})
			return state.allMoney = all_money
		}

	},
	actions: {
		// lazy loading openid

	}
})

export default store
